package utils.utilcode.blankj.com.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

import static utils.utilcode.blankj.com.myapplication.DisplayUtil.getDisplayMetrics;

/**
 * Created by Administrator on 2017-12-05.
 */

public class WheelView extends View {

    private Context context;
    private int TotalHeight;
    private Paint mLinePaint;
    private List<String> list = new ArrayList<>();
    private Paint mTextPaint;
    private Rect mBound = new Rect();
    private GestureDetector gestureDetector;
    private Scroller mScroller;

    public WheelView(Context context) {
        this(context, null);
    }

    public void setData(List<String> list) {
        this.list = list;
    }

    public WheelView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WheelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        initList();
        gestureDetector = new GestureDetector(context, new MyOnGestureListener());
        mScroller = new Scroller(context,new BounceInterpolator());

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(Color.RED);
        mLinePaint.setStrokeWidth(dip2px(context, 2));
        //画文字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(sp2px(context, 16));
        //中心点为底部中心
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mLinePaint.setColor(Color.YELLOW);
        mTextPaint.getTextBounds(list.get(0), 0, list.get(0).length(), mBound);//你可以得到能够包裹文字的最小矩形

//以父控件为坐标原点，中心点为文字的左下角，第二个参数为x坐标，第三个为y坐标
//绘制中心点
//        canvas.drawText(mMTitleText, getMeasuredWidth() / 2, getMeasuredHeight() / 2 + mBound.height() / 2, mPaintText);


    }

    private void initList() {
        list.add("新余");
        list.add("南昌");
        list.add("宜春");
        list.add("丰城");
    }

    public static int dip2px(Context c, float dpValue) {
        final float scale = getDisplayMetrics(c).density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int sp2px(Context c, float spValue) {
        float fontScale = getDisplayMetrics(c).scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("WheelView","dis"+getScrollY());

        //画线
        canvas.drawLine(0, getMeasuredHeight() / 3+getScrollY(), getMeasuredWidth(), getMeasuredHeight() / 3+getScrollY(), mLinePaint);
        canvas.drawLine(0, getMeasuredHeight() * 2 / 3+getScrollY(), getMeasuredWidth(), getMeasuredHeight() * 2 / 3+getScrollY(), mLinePaint);
        //画文字

//       画中心点
      Log.e("WheelView", "getMeasuredWidth() " + getMeasuredWidth() + " getMeasuredHeight " + getMeasuredHeight());
        canvas.drawText(list.get(0), getMeasuredWidth() / 2, getMeasuredHeight() / 2 + mBound.height() / 2, mTextPaint);

        //画上下
        canvas.drawText(list.get(1), getMeasuredWidth() / 2, getMeasuredHeight() / 6 + mBound.height() / 2, mTextPaint);
        canvas.drawText(list.get(2), getMeasuredWidth() / 2, getMeasuredHeight() * 5 / 6 + mBound.height() / 2, mTextPaint);
        canvas.drawText(list.get(3), getMeasuredWidth() / 2, getMeasuredHeight() * 7 / 6 + mBound.height() / 2, mTextPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mScroller.startScroll(0,0,0,-getMeasuredHeight()/3,1000);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);

      switch (event.getAction()){
          case MotionEvent.ACTION_UP:
              Log.e("WheelView","???");
              if(getScrollY()<getMeasuredHeight()/3){
                  mScroller.startScroll(0,0,0,-getMeasuredHeight()/3,1000);
                  postInvalidate();
              }
              break;
          default:
              break;
      }

        return true;
    }

    class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        //单机
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return super.onSingleTapConfirmed(e);
        }

        //拖动 e1为按下时的坐标(按下就不变)，e2是移动时的坐标,0distanceX,0distanceX忽略吧
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            boolean down_move=false;
//            Log.e("MyOnGestureListener", e1.getY() + "  " + e2.getY());
//            Log.e("MyOnGestureListener","getScrollY"+getScrollY());
            if( e2.getY()>e1.getY()){
                down_move=true;
            }
            //移动偏移量
            int disY = (int) (e2.getY() - e1.getY());
            //正上
            if(down_move&&(getScrollY()<-getMeasuredHeight()/3)){

            }else {
                scrollBy(0, -disY);
            }
            invalidate();

            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        //Touch了滑动一点距离后，up时触发   e1为按下时的坐标(按下就不变)，e2是移动时的坐标,   velocityX X轴上的移动速度，像素/秒 ；velocityY Y轴上的移动速度，像素/秒
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.e("MyOnGestureListener","velocityY     "+velocityY);


            return super.onFling(e1, e2, velocityX, velocityY);
        }
       
    }

    @Override
    public void computeScroll() {
        //先判断mScroller滚动是否完成
        Log.e("WheelView","mScroller.computeScrollOffset()   "+mScroller.computeScrollOffset());

        if (mScroller.computeScrollOffset()) {
            //这里调用View的scrollTo()完成实际的滚动
            scrollTo(0, mScroller.getCurrY());
            //必须调用该方法，否则不一定能看到滚动效果
            postInvalidate();
        }
    }
}
